﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web;
using System.IO;

namespace OF.Common
{
    /// <summary>
    /// 项目名称：红包中国2.0
    /// 模块名称：公共类库
    /// 文件名称：脚本工具类
    /// 作    者：LPZ
    /// 创建时间：2013-1-15
    /// </summary>
    public class ScriptTagUtil
    {
        /// <summary>
        /// 呈现脚本的容器控件
        /// </summary>
        readonly TemplateControl control;
        /// <summary>
        /// 脚本缓存
        /// Key：为脚本相对路径
        /// Value：为声明脚本的HTML
        /// </summary>
        readonly Dictionary<string, string> scripts;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="control">模版控件</param>
        public ScriptTagUtil(TemplateControl control)
        {
            this.control = control;
            scripts = new Dictionary<string, string>();
        }

        /// <summary>
        /// 添加脚本
        /// </summary>
        /// <param name="relPath">脚本路径</param>
        /// <returns></returns>
        public bool AppendScript(string relPath)
        {
            bool isAppend = false;

            string plPath = HttpContext.Current.Server.MapPath(relPath);

            //查看是否有脚本
            if (File.Exists(plPath))
            {
                if (!scripts.ContainsKey(relPath))
                {
                    var s = Path.GetExtension(relPath);
                    if (s != null)
                    {
                        string extension = s.ToLower();
                        //根据后缀名区分添加的是css还是js的声明
                        if (extension == ".css")
                        {
                            scripts.Add(relPath, CreateCssTag(relPath));
                            isAppend = true;
                        }
                        else if (extension == ".js")
                        {
                            scripts.Add(relPath, CreateJsTag(relPath));
                            isAppend = true;
                        }
                    }
                }
            }
            return isAppend;
        }

        /// <summary>
        /// 呈现所有脚本的声明html
        /// </summary>
        /// <param name="headerControl">控件</param>
        public void Render(Control headerControl)
        {
            if (scripts.Count > 0 && headerControl != null)
            {
                string s = @"<!-- 脚本工具类自动注入脚本开始 -->" +
                    string.Join("\r\n", scripts.Values.ToArray()) +
                           "<!-- 脚本工具类自动注入脚本结束 -->";
                headerControl.Controls.AddAt(0, new LiteralControl(s));
                scripts.Clear();
            }
        }

        /// <summary>
        /// 创建JS标签
        /// </summary>
        /// <param name="relPath">js相对路径</param>
        /// <returns>JS引用标签</returns>
        private string CreateJsTag(string relPath)
        {
            return string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>"
                , control.ResolveUrl(relPath));
        }

        /// <summary>
        /// 创建CSS标签
        /// </summary>
        /// <param name="relPath">CSS相对路径</param>
        /// <returns>CSS引用标签</returns>
        private string CreateCssTag(string relPath)
        {
            return string.Format("<link href=\"{0}\" type=\"text/css\" rel=\"stylesheet\" />"
                , control.ResolveUrl(relPath));
        }
    }
}
